import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/*
 * 列表
 */
class HistoryList extends StatelessWidget {
// 列表内容
  final List<Widget> _historyList = List.generate(20, (int index) {
    return HistoryItem(
      index.toString(),
    );
  });

  final EasyRefreshController _controller = EasyRefreshController();

  @override
  Widget build(BuildContext context) {
    return Container(
      child: EasyRefresh(
        header: ClassicalHeader(
            refreshText: '下拉加载更多',
            refreshReadyText: '松手加载更多',
            refreshingText: '正在加载',
            refreshedText: '加载完成',
            refreshFailedText: '加载失败，请检查您的网络连接',
            showInfo: false),
        controller: _controller,
        onRefresh: () async {
          await Future.delayed(Duration(seconds: 2), () {
            _controller.resetLoadState();
          });
        },
        child: ListView(
          children: _historyList,
        ),
      ),
    );
  }
}

/*
 * 列表项
 */
class HistoryItem extends StatelessWidget {
  final String title;
  HistoryItem(this.title);
  // 跳转到聊天界面
  @override
  Widget build(BuildContext context) {
    void _tabHandle() {
      Navigator.of(context).pushNamed('/chat');
    }

    return Material(
      child: InkWell(
        onTap: _tabHandle,
        child: Container(
          height: ScreenUtil.getInstance().setWidth(192),
          padding: EdgeInsets.only(left: 4, right: 4),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              // 头像区域
              Expanded(
                flex: 1,
                child: Image.asset(
                  'static/avator.png',
                  width: 38,
                  height: 38,
                ),
              ),
              // 信息区域
              Expanded(
                  flex: 9,
                  child: Container(
                      margin: EdgeInsets.only(left: 8),
                      decoration: BoxDecoration(
                          border: Border(
                              bottom: BorderSide(
                        color: Colors.black26,
                        width: 0.5,
                      ))),
                      child: Row(
                        children: <Widget>[
                          Expanded(
                            flex: 8,
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Text('消息标题',
                                    style: Theme.of(context).textTheme.title),
                                Text(
                                  '用户发送的消息预览用户发送的消息预览用户发送的消息预览用户发送的消息预览',
                                  overflow: TextOverflow.ellipsis,
                                  style: Theme.of(context).textTheme.subtitle,
                                )
                              ],
                            ),
                          ),
                          Expanded(
                            flex: 1,
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: <Widget>[
                                Text(
                                  '17:33',
                                  style: Theme.of(context)
                                      .textTheme
                                      .subtitle
                                      .copyWith(
                                        fontSize:
                                            ScreenUtil.getInstance().setSp(38),
                                      ),
                                ),
                                Icon(
                                  Icons.notifications_off,
                                  size: ScreenUtil.getInstance().setSp(50),
                                  color: Color(0xff999999),
                                ),
                              ],
                            ),
                          )
                        ],
                      ))),
            ],
          ),
        ),
      ),
    );
  }
}
